package com.ruoyi.project.system.product.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.region.domain.Region;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.product.domain.Product;
import com.ruoyi.project.system.product.service.IProductService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-03-25
 */
@Controller
@RequestMapping("/system/product")
public class ProductController extends BaseController {
    private String prefix = "system/product";

    @Autowired
    private IProductService productService;

    @RequiresPermissions("system:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "导出商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Product product)
    {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{idProduct}")
    public String edit(@PathVariable("idProduct") Long idProduct, ModelMap mmap)
    {
        Product product = productService.selectProductById(idProduct);
        mmap.put("product", product);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Product product)
    {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }

    @RequestMapping("/getPrice")
    @ResponseBody
    public AjaxResult list(Long idProduct )
    {
        startPage();
        Long price = productService.getPriceById(idProduct);
        //List<Region> list = regionService.selectRegionByParentId(parentId);
        return AjaxResult.success("成功",price);
    }

    @RequestMapping("/getGoods")
    @ResponseBody
    public TableDataInfo getGoods()
    {
        startPage();
        List<Product> list = productService.queryProductList();
        return getDataTable(list);
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/year")
    public String year()
    {
        return prefix + "/yearSale";
    }

    @PostMapping("/getYear")
    @ResponseBody
    public TableDataInfo getYear(Product product)
    {
        startPage();
        if (product.getsTime() == null && product.getsTime() == null){
            product.setsTime( DateUtils.getYearFirst(DateUtils.getYear()));
            product.seteTime( DateUtils.getYearLast(DateUtils.getYear()));
        }else {
            if (product.getsTime() != null && product.geteTime() == null){
                product.seteTime( DateUtils.getYearLast(DateUtils.getYear()));
            }else if (product.geteTime() != null && product.getsTime() == null){
                product.setsTime( DateUtils.getYearFirst(DateUtils.getYear()));
            }
        }
        List<Product> list = productService.getYearSale(product);
        return getDataTable(list);
    }
}
